package com;
//增删改的批处理_手动提交
import sun.java2d.loops.FontInfo;

import java.sql.*;

public class Demo6 {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        PreparedStatement pps = null;
        Savepoint abc =null;
        try {
            //1.加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获得链接
            String userName="root";
            String passWord="123456";
            String url="jdbc:mysql://localhost:3306/yhp3?serverTimezone=UTC";
            connection = DriverManager.getConnection(url,userName,passWord);
            connection.setAutoCommit(false);//设置成手动提交
            //3.定义sql,创建状态通道(进行sql语句的发送)
            statement = connection.createStatement();
            //4.定义sql
            String sql1 = "insert into teacher(tname) values('张三a')";
            statement.addBatch(sql1);
            String sql2 = "insert into teacher(tname) values('张三b')";
            statement.addBatch(sql2);
            String sql3 = "insert into teacher(tname) values('张三c')";
            statement.addBatch(sql3);
            String sql4 = "insert into teacher(tname) values('张三d')";
            statement.addBatch(sql4);

            int[] ints = statement.executeBatch();

            connection.commit();//手动提交
            for (int anInt:ints){
                System.out.println("anInt = "+anInt);
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            }catch (SQLException throwable){
                throwable.printStackTrace();
            }
        } finally {
            //关闭资源
            try {
                if(statement != null){
                    statement.close();
                }
                if (connection != null){
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
